﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.UI.Models;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.Configuration;
using MethodWorx.CMS.UI.Models.Shared;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.Requests;
using MethodWorx.Core.DomainModel;

namespace MethodWorx.CMS.UI.Controllers
{
    public class ConfigureContentTemplateController : Controller
    {
        ContentTemplateService service;

        public ConfigureContentTemplateController(ContentTemplateService service)
        {
            this.service = service;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult BasicDetails(Guid id)
        {
            return View("BasicDetails", new ConfigureContentTemplateModel(this.service, id));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult BasicDetails(Guid id, FormCollection form)
        {
            UpdateContentTemplateBasicDetailsRequest request = new UpdateContentTemplateBasicDetailsRequest(this.service, id);
            this.TryUpdateModel(request);

            if (this.ModelState.IsValid)
            {
                //  complete the request
                try
                {
                    request.Complete();
                    return RedirectToAction("Refresh", "Schema", new { keepOpen = false });
                }
                catch (BusinessRuleException bre)
                {
                    bre.AddToModelState(this.ModelState);
                }
            }

            return View("BasicDetails", new ConfigureContentTemplateModel(request));
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Properties(Guid id, Guid? pageId, Guid? contentItemId, string submit, int? index)
        {
            
            this.ViewData["pageId"] = pageId;
            this.ViewData["contentItemId"] = contentItemId;
            var model = new ConfigurePropertiesModel(service.GetContentTemplateById(id), 0);

            if (submit == "remove")
            {
                model.Remove(index.Value);
                //
                //  ok, we are valid, so lets save
                UpdateContentTemplatePropertiesRequest request = new UpdateContentTemplatePropertiesRequest(this.service, id);
                request.Clear();
                foreach (var property in model.Properties)
                {
                    request.Add(property.Name, property.Mandatory, property.Type, (ICustomPropertyDetails)property.Custom);
                }
                request.Complete();
                this.ViewData["message"] = "The property was removed";
            }

            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Properties(Guid id, int? selectedPropertyIndex, PropertyDetails<object>[] propertyDetails, string submit, Guid?pageId, Guid?contentItemId)
        {
            var model = new ConfigurePropertiesModel(service.GetContentTemplateById(id), selectedPropertyIndex ?? 0);
            model.Properties = propertyDetails ?? new PropertyDetails<object>[] { };

            switch (submit)
            {
                case "add":
                    model.Add();
                    break;
                case "save":
                    if (this.ModelState.IsValid)
                    {
                        //
                        //  ok, we are valid, so lets save
                        UpdateContentTemplatePropertiesRequest request = new UpdateContentTemplatePropertiesRequest(this.service, id);
                        request.Clear();
                        foreach (var property in model.Properties)
                        {
                            request.Add(property.Name, property.Mandatory, property.Type, (ICustomPropertyDetails)property.Custom);
                        }
                        request.Complete();
                        this.ViewData["message"] = "Page Template basic details have been successfully saved";
                    }
                    break;
            }
            this.ViewData["pageId"] = pageId;
            this.ViewData["contentItemId"] = contentItemId;
            return View(model);
        }

    }
}
